import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import type { JobInfo } from '../types/jobs'

export const useJobsStore = defineStore('jobs', () => {
  const jobList = ref<JobInfo[]>([])
  // 默认活跃时间：一周内
  const validTime = ref<number>(86400 * 1000 * 7)
  // 设置职位信息
  const appenJobs = (jobs: JobInfo[]) => {
    jobList.value = [...jobList.value, ...jobs]
  }
  // 清除职位信息
  const clearJobs = () => {
    jobList.value = []
  }

  // 筛选出近期活跃的职位
  const activeJobs = computed(() => {
    const timeNow = Date.now()
    return jobList.value.filter(item => {
      return item.lastModifyTime > timeNow - validTime.value
    })
  })
  return { jobList, appenJobs, clearJobs, activeJobs }
})